{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "02 basic grammar.ipynb",
      "version": "0.3.2",
      "views": {},
      "default_view": {},
      "provenance": [],
      "collapsed_sections": [
        "SMatNX_8xEmZ",
        "xVkG5uoLqHBm",
        "YSBesxOFoxce"
      ]
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "metadata": {
        "id": "WTV8tq8ebYvc",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "# 标识符\n",
        "\n",
        "* 在 Python 里，标识符由字母、数字、下划线组成。\n",
        "\n",
        "* 在 Python 中，所有标识符可以包括英文、数字以及下划线(_)，但不能以数字开头。\n",
        "\n",
        "* Python 中的标识符是区分大小写的。\n",
        "\n",
        "* 以下划线开头的标识符是有特殊意义的。以单下划线开头 _foo 的代表不能直接访问的类属性，需通过类提供的接口进行访问，不能用 from xxx import * 而导入；\n",
        "\n",
        "* 以双下划线开头的 `__foo` 代表类的私有成员；以双下划线开头和结尾的 `__foo__ `代表 Python 里特殊方法专用的标识，如  `__init__()` 代表类的构造函数。\n",
        "\n",
        "* Python 可以同一行显示多条语句，方法是用分号` ; `分开，\n",
        "如：\n",
        "\n"
      ]
    },
    {
      "metadata": {
        "id": "2y_yO89ab7kA",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 134
        },
        "outputId": "059c73b3-0bea-4b4c-837e-4b34e8605f71",
        "executionInfo": {
          "status": "error",
          "timestamp": 1529594049885,
          "user_tz": -480,
          "elapsed": 2148,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "a=\"a\"\n",
        "_b1=\"bb\"\n",
        "print(\"hello, \"); print(\"world!\")\n",
        "\n",
        "a/b = \"ccc\"  # 这里错误，不能包含 字母、数字、下划线 以外的字符"
      ],
      "execution_count": 53,
      "outputs": [
        {
          "output_type": "error",
          "ename": "SyntaxError",
          "evalue": "ignored",
          "traceback": [
            "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-53-7c067522f272>\"\u001b[0;36m, line \u001b[0;32m5\u001b[0m\n\u001b[0;31m    a/b = \"ccc\"  # 这里错误，不能包含 字母、数字、下划线 以外的字符\u001b[0m\n\u001b[0m                                            ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m can't assign to operator\n"
          ]
        }
      ]
    },
    {
      "metadata": {
        "id": "zgUQfbR3cASn",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "# 保留字\n",
        "\n",
        "下面的列表显示了在Python中的保留字。这些保留字不能用作常数或变数，或任何其他标识符名称。\n",
        "\n",
        "所有 Python 的关键字只包含小写字母。\n",
        "\n",
        "   保留字 | 保留字  |  保留字   |\n",
        "---|---|---\n",
        "and\t| exec|\tnot\n",
        "assert\t| finally\t| or\n",
        "break |\tfor\t| pass\n",
        "class |\tfrom |\tprint\n",
        "continue |\tglobal\t |raise\n",
        "def\t| if |\treturn\n",
        "del |\timport |\ttry\n",
        "elif\t| in\t | while\n",
        "else\t| is\t| with\n",
        "except |\tlambda\t| yield\n",
        "\n",
        "# 行和缩进\n",
        "\n",
        "学习 Python 与其他语言最大的区别就是，Python 的代码块不使用大括号 {} 来控制类，函数以及其他逻辑判断。python 最具特色的就是用缩进来写模块。\n",
        "\n",
        "缩进的空白数量是可变的，但是所有代码块语句必须包含相同的缩进空白数量，这个必须严格执行。如下所示：\n"
      ]
    },
    {
      "metadata": {
        "id": "ucYbbHDOcigr",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "3ea06828-14dc-4033-fe19-c1420bde3375",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529418698276,
          "user_tz": -480,
          "elapsed": 4596,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "if True:\n",
        "  print(\"True\")\n",
        "else:\n",
        "  print(\"False\")"
      ],
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "True\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "We6ysnGAdQxo",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 134
        },
        "outputId": "116d2184-8a01-440b-9785-0d7c541b6909",
        "executionInfo": {
          "status": "error",
          "timestamp": 1529418755974,
          "user_tz": -480,
          "elapsed": 2984,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "if True:\n",
        "    print( \"Answer\")\n",
        "    print( \"True\")\n",
        "else:\n",
        "    print( \"Answer\")\n",
        "    # 没有严格缩进，在执行时会报错\n",
        "  print( \"False\")"
      ],
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "error",
          "ename": "IndentationError",
          "evalue": "ignored",
          "traceback": [
            "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-6-90ba842299af>\"\u001b[0;36m, line \u001b[0;32m7\u001b[0m\n\u001b[0;31m    print( \"False\")\u001b[0m\n\u001b[0m                   ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m unindent does not match any outer indentation level\n"
          ]
        }
      ]
    },
    {
      "metadata": {
        "id": "UXG_TOHzddFz",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "**IndentationError: unindent does not match any outer indentation level** 错误表明，你使用的缩进方式不一致，有的是 tab 键缩进，有的是空格缩进，改为一致即可。\n",
        "\n",
        "如果是 IndentationError: unexpected indent 错误, 则 python 编译器是在告诉你\"Hi，老兄，你的文件里格式不对了，可能是tab和空格没对齐的问题\"，所有 python 对格式要求非常严格。\n",
        "\n",
        "因此，在 Python 的代码块中必须使用相同数目的行首缩进空格数。\n",
        "\n",
        "建议你在每个缩进层次使用 单个制表符 或 两个空格 或 四个空格 , 切记不能混用\n",
        "\n",
        "\n",
        "\n",
        "# 多行语句\n",
        "\n",
        "Python语句中一般以新行作为语句的结束符。\n",
        "\n",
        "但是我们可以使用斜杠（ \\）将一行的语句分为多行显示，如下所示："
      ]
    },
    {
      "metadata": {
        "id": "mnrmSkWIdzY-",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "cell_type": "code",
      "source": [
        "item_one='1'\n",
        "item_two = '2'\n",
        "item_three = '3'\n",
        "\n",
        "total = item_one + \\\n",
        "        item_two + \\\n",
        "        item_three"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "7nIz1KjKd1HN",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "语句中包含 [], {} 或 () 括号就不需要使用多行连接符。如下实例："
      ]
    },
    {
      "metadata": {
        "id": "wG83-EHaeBB6",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "cell_type": "code",
      "source": [
        "days = ['Monday', 'Tuesday', 'Wednesday',\n",
        "        'Thursday', 'Friday']"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "SkqHP28XeCU5",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "# python 引号"
      ]
    },
    {
      "metadata": {
        "id": "b_uDUSv8eIip",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Python 可以使用引号( ' )、双引号( \" )、三引号( ''' 或 \"\"\" ) 来表示字符串，引号的开始与结束必须的相同类型的。\n",
        "\n",
        "其中三引号可以由多行组成，编写多行文本的快捷语法，常用于文档字符串，在文件的特定地点，被当做注释。\n"
      ]
    },
    {
      "metadata": {
        "id": "4s0jVsfWeNYa",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          }
        }
      },
      "cell_type": "code",
      "source": [
        "word = 'word'\n",
        "sentence = \"这是一个句子。\"\n",
        "paragraph = \"\"\"这是一个段落。\n",
        "包含了多个语句\"\"\""
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "BfVERAuneNrn",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "# Python 注释\n",
        "\n",
        "python中单行注释采用 # 开头。注释可以在语句或表达式行末。\n",
        "\n",
        "python 中多行注释使用三个单引号(''')或三个双引号(\"\"\")。\n"
      ]
    },
    {
      "metadata": {
        "id": "xy8RZINgedCL",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 51
        },
        "outputId": "d6170703-76fa-4684-a175-e1aa097340fa",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529419052613,
          "user_tz": -480,
          "elapsed": 2294,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "# 第一个注释\n",
        "print(\"Hello, Python!\")  # 第二个注释\n",
        "\n",
        "\n",
        "'''\n",
        "这是多行注释，使用单引号。\n",
        "这是多行注释，使用单引号。\n",
        "这是多行注释，使用单引号。\n",
        "'''\n",
        "\n",
        "\"\"\"\n",
        "这是多行注释，使用双引号。\n",
        "这是多行注释，使用双引号。\n",
        "这是多行注释，使用双引号。\n",
        "\"\"\"\n",
        "print(\"没有看到错误就正确了哟！恭喜你\")"
      ],
      "execution_count": 12,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Hello, Python!\n",
            "没有看到错误就正确了哟！恭喜你\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "gNMGZp5peidA",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "# python 空行\n",
        "\n",
        "函数之间或类的方法之间用空行分隔，表示一段新的代码的开始。类和函数入口之间也用一行空行分隔，以突出函数入口的开始。\n",
        "\n",
        "空行与代码缩进不同，空行并不是Python语法的一部分。书写时不插入空行，Python解释器运行也不会出错。但是空行的作用在于分隔两段不同功能或含义的代码，便于日后代码的维护或重构。\n",
        "\n",
        "记住：空行也是程序代码的一部分。\n",
        "\n"
      ]
    },
    {
      "metadata": {
        "id": "TMT7PaiXe09U",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "# 数据类型\n",
        "\n",
        "对于数据类型这里只做简要介绍，随后会详细介绍每种类型。\n",
        "\n",
        "\n",
        "对象类型 | 实例 \n",
        "---|--- \n",
        "Numbers(数值) | 123, 3.4, 3+4j, Ob111, Decimal(), Fraction()\n",
        "Strings(字符) | '你好', \"spam\", b'\\x01c', u'sp\\xc4m'\n",
        "Lists(列表) | [1,[2,'three'], 4.5], list(range(10))\n",
        "Dictionaries (字典) | {'food':'orange', 'taste': 'yum'}, dict(hours=10)\n",
        "Tuples(元组) | (1,'yellow', 4, 'U'), tuple('spam'), namedtuple\n",
        "Files(文件) | open('eggs.text'), open('/data/aaa.bin', 'wb')\n",
        "Sets(集合) | set('abc'), {'a', 'b', 'c'}\n",
        "Other core types(其他核心类型) | Booleans, types, None\n",
        "Program unit types(程序单元类型) | Functios, modules, classes\n",
        "Implementation-related types(实施相关) | 编译的代码(.pyc)，stack tracebacks\n",
        "\n",
        "\n"
      ]
    },
    {
      "metadata": {
        "id": "9UsUJm7Rzk9H",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "\n",
        "## 数值"
      ]
    },
    {
      "metadata": {
        "id": "us_BV6AlnXsT",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "a001b9ea-a8c0-4216-dbc5-3393b282444e",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529421392946,
          "user_tz": -480,
          "elapsed": 40407,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "123"
      ],
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "123"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 15
        }
      ]
    },
    {
      "metadata": {
        "id": "LfggplyNnYtE",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "fd103f1d-65e4-4876-c83f-7c329d05a5c6",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529421394668,
          "user_tz": -480,
          "elapsed": 1531,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "123+222"
      ],
      "execution_count": 16,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "345"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 16
        }
      ]
    },
    {
      "metadata": {
        "id": "HET-z-PqnbGk",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "1843c65d-da45-49c5-ee7b-de294bf34b23",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529421402391,
          "user_tz": -480,
          "elapsed": 4271,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "1.4*4"
      ],
      "execution_count": 17,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "5.6"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 17
        }
      ]
    },
    {
      "metadata": {
        "id": "thkyZ1nAncQZ",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "c6b5112d-2a79-4da2-d417-e03004244a50",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529421403779,
          "user_tz": -480,
          "elapsed": 1184,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "2**200"
      ],
      "execution_count": 18,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "1606938044258990275541962092341162602522202993782792835301376"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 18
        }
      ]
    },
    {
      "metadata": {
        "id": "Ll-qA9tZnd96",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "0574f99f-fdfb-4957-ad51-76152998490d",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529421404712,
          "user_tz": -480,
          "elapsed": 734,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "# 去数字长度\n",
        "len(str(2**100000))"
      ],
      "execution_count": 19,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "30103"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 19
        }
      ]
    },
    {
      "metadata": {
        "id": "UIsqsdE-niCY",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "25f53661-61e9-4624-b64b-1375fd1bdaa6",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529421419003,
          "user_tz": -480,
          "elapsed": 1541,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "3.1415 * 2"
      ],
      "execution_count": 23,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "6.283"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 23
        }
      ]
    },
    {
      "metadata": {
        "id": "FBQGiwtlnnIC",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "0b5720fa-8792-4f4a-82f2-3e6d824f9793",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529421417385,
          "user_tz": -480,
          "elapsed": 3263,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "# 使用 print 输出\n",
        "print(3.1415 * 2)"
      ],
      "execution_count": 22,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "6.283\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "tizeSvLenqEU",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "2a19252d-02d0-4c9f-cc0b-60f738301768",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529421522533,
          "user_tz": -480,
          "elapsed": 10983,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "# 使用 math 数据模块\n",
        "import math\n",
        "\n",
        "math.pi"
      ],
      "execution_count": 26,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "3.141592653589793"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 26
        }
      ]
    },
    {
      "metadata": {
        "id": "IwbGVv4fnzD9",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "4282c9f7-e4ab-4167-d69d-13939b83164f",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529421531492,
          "user_tz": -480,
          "elapsed": 8845,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "math.sqrt(85)"
      ],
      "execution_count": 27,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "9.219544457292887"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 27
        }
      ]
    },
    {
      "metadata": {
        "id": "iEY2Vkl2n1LN",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "bfcafa41-2ee7-4065-af1f-39b5ef064d2e",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529590057524,
          "user_tz": -480,
          "elapsed": 1928,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "# 随机数\n",
        "import random as rd\n",
        "\n",
        "rd.random()"
      ],
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0.9251197304813635"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 9
        }
      ]
    },
    {
      "metadata": {
        "id": "7MpytIERn5TR",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "4fc564cd-8271-4d41-85b4-10e13fbadba7",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529591648239,
          "user_tz": -480,
          "elapsed": 1502,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "# 随机选择一个元素\n",
        "rd.choice([1,2,3,4])"
      ],
      "execution_count": 33,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "2"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 33
        }
      ]
    },
    {
      "metadata": {
        "id": "SMatNX_8xEmZ",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "### 数值格式化输出"
      ]
    },
    {
      "metadata": {
        "id": "t17YZB6xxJJO",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 51
        },
        "outputId": "38015848-fca5-4732-9d44-5cd5ff34f1a3",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529591701578,
          "user_tz": -480,
          "elapsed": 1477,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "print('{:,.2f}'.format(29688.2578))\n",
        "print('%.2f | %+05d' % (3.1415926, -42))"
      ],
      "execution_count": 35,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "29,688.26\n",
            "3.14 | -0042\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "xVkG5uoLqHBm",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "## 字符串\n",
        "\n",
        "字符串是要使用引号引起来，可以使以下三种的其中一种：`''、\"\"、\"\"\"\"\"\"`。其中 `\"\"\"\"\"\"` 可以包含多行文本。并且字符串是不可以改变的。"
      ]
    },
    {
      "metadata": {
        "id": "zb9xN-w6n8Ox",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "c891d9dd-69e2-4c26-bee2-4d2717b87838",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529590391128,
          "user_tz": -480,
          "elapsed": 2557,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "S = \"Yellow\"\n",
        "len(S)  # 获取文本长度"
      ],
      "execution_count": 12,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "6"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 12
        }
      ]
    },
    {
      "metadata": {
        "id": "boGLTExYoLis",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 102
        },
        "outputId": "89e5ecc2-030c-4f53-c360-759f6a6f14f3",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529590479122,
          "user_tz": -480,
          "elapsed": 4208,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "# 获取字符串特定位置的字符\n",
        "print(S[0])\n",
        "print(S[1])\n",
        "print(S[-1])  # 可以使用负数，表示从右向左\n",
        "print(S[-2])\n",
        "print(S[len(S)-1]) # 这个和 print(S[-1]) 等效"
      ],
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Y\n",
            "e\n",
            "w\n",
            "o\n",
            "w\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "QwiAgYz6oVTw",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 136
        },
        "outputId": "a9a23e47-75ce-45e7-c47a-1d10f8912a14",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529590653398,
          "user_tz": -480,
          "elapsed": 954,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "print(S)\n",
        "# 返回字符串子串\n",
        "print(S[1:3])\n",
        "print(S[1:])\n",
        "print(S)  # 返回字符串不会更改字符串的值\n",
        "\n",
        "print(S[0:3])\n",
        "print(S[:-1])\n",
        "print(S[:])\n"
      ],
      "execution_count": 17,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Yellow\n",
            "el\n",
            "ellow\n",
            "Yellow\n",
            "Yel\n",
            "Yello\n",
            "Yellow\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "M4CObd6UojJT",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 51
        },
        "outputId": "52166dda-ebd2-454f-fa8b-7bd82abaee37",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529590733057,
          "user_tz": -480,
          "elapsed": 3456,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "# 拼接两个字符串\n",
        "print(S + \"abc\")\n",
        "\n",
        "# 重复输出 N 遍字符串\n",
        "print(S*8)"
      ],
      "execution_count": 18,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Yellowabc\n",
            "YellowYellowYellowYellowYellowYellowYellowYellow\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "YSBesxOFoxce",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "### 不可更改性"
      ]
    },
    {
      "metadata": {
        "id": "3s7U7wKgo1I_",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 219
        },
        "outputId": "10755d18-617d-47e2-eaf4-e12371dcdb45",
        "executionInfo": {
          "status": "error",
          "timestamp": 1529590772263,
          "user_tz": -480,
          "elapsed": 1022,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "print(S)\n",
        "\n",
        "# 此语句将会报错\n",
        "S[0] = 'z'"
      ],
      "execution_count": 20,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Yellow\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "error",
          "ename": "TypeError",
          "evalue": "ignored",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-20-798812957f79>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0;31m# 此语句将会报错\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mS\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'z'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment"
          ]
        }
      ]
    },
    {
      "metadata": {
        "id": "bfQt3AUso4wY",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "a82f35b0-2f8f-45dd-a11b-8e68cfe2843f",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529590905033,
          "user_tz": -480,
          "elapsed": 1697,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "# 但是我们可以使用 子串 和 字符串拼接，形成新的字符串\n",
        "S = 'z' + S[1:]\n",
        "print(S)"
      ],
      "execution_count": 21,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "zellow\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "rb7Bv5iro9AK",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 51
        },
        "outputId": "5434f5d5-ba68-4ab5-b497-8f35b65d613f",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529590987969,
          "user_tz": -480,
          "elapsed": 1215,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "# 或者可以使用 list 进行字符串的修改，但是这个也是形成新的字符串，不会修改原始字符串\n",
        "a = \"abcdefg\"\n",
        "L = list(a)\n",
        "L\n",
        "L[1] = 'c'\n",
        "print(''.join(L))\n",
        "print(a)"
      ],
      "execution_count": 24,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "accdefg\n",
            "abcdefg\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "Y035vDIqpCuD",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "更多关于字符串的知识，后面会详细介绍"
      ]
    },
    {
      "metadata": {
        "id": "OadESngMqxE_",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "### 格式化"
      ]
    },
    {
      "metadata": {
        "id": "9sf7akfaq2fg",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 68
        },
        "outputId": "1023f521-029f-47ad-9e88-7000c2830851",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529591765372,
          "user_tz": -480,
          "elapsed": 3753,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "print('%s, eggs, and %s' % ('spam', 'SPAM!'))\n",
        "print('{0}, eggs, and {1}'.format('spam', 'SPAM!'))\n",
        "print('{}, eggs, and {}'.format('spam', 'SPAM!'))"
      ],
      "execution_count": 36,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "spam, eggs, and SPAM!\n",
            "spam, eggs, and SPAM!\n",
            "spam, eggs, and SPAM!\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "TyeQQGkctdpB",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "# 列表\n"
      ]
    },
    {
      "metadata": {
        "id": "TBMom6tRwmux",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 51
        },
        "outputId": "5b9b39f7-e3fa-4283-dcac-b20fc3e7c74d",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529591260919,
          "user_tz": -480,
          "elapsed": 1171,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "L = [123, 'spam', 1.33]\n",
        "print(L)\n",
        "print(len(L)) # 获取列表长度"
      ],
      "execution_count": 25,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[123, 'spam', 1.33]\n",
            "3\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "2hEePXguwspq",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 51
        },
        "outputId": "5dad11a0-1479-4c6e-cdb3-af8a2d2100b5",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529591313431,
          "user_tz": -480,
          "elapsed": 1648,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "# 获取列表值\n",
        "print(L[0])\n",
        "print(L[:-1]) # 返回一个子列表"
      ],
      "execution_count": 26,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "123\n",
            "[123, 'spam']\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "ZJH4XK50wvnv",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 51
        },
        "outputId": "6f50f876-8bf4-4963-d26e-cf025f5d1ded",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529591476473,
          "user_tz": -480,
          "elapsed": 1814,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "print(L + [4,5,6]) # 列表拼接\n",
        "print(L*2)         "
      ],
      "execution_count": 29,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[123, 'spam', 1.33, 4, 5, 6]\n",
            "[123, 'spam', 1.33, 123, 'spam', 1.33]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "U8bzSa-Uwalw",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 236
        },
        "outputId": "3265c4f3-7a07-4cc8-c5ea-dcdf9159e145",
        "executionInfo": {
          "status": "error",
          "timestamp": 1529591542590,
          "user_tz": -480,
          "elapsed": 2239,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "# 检查边界\n",
        "print(L)\n",
        "print(L[1])\n",
        "print(L[99]) # 这里会报错误"
      ],
      "execution_count": 31,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[123, 'spam', 1.33]\n",
            "spam\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "error",
          "ename": "IndexError",
          "evalue": "ignored",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-31-d40083741a4a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mL\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mL\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mL\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m99\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# 这里会报错误\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mIndexError\u001b[0m: list index out of range"
          ]
        }
      ]
    },
    {
      "metadata": {
        "id": "KgwDTxZmxiwc",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 168
        },
        "outputId": "8903507b-e653-4af5-8013-92c727a521e8",
        "executionInfo": {
          "status": "error",
          "timestamp": 1529591577445,
          "user_tz": -480,
          "elapsed": 1445,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "L[99] = 1 # 赋值也会报错"
      ],
      "execution_count": 32,
      "outputs": [
        {
          "output_type": "error",
          "ename": "IndexError",
          "evalue": "ignored",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-32-a8d940cf9021>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mL\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m99\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m1\u001b[0m \u001b[0;31m# 赋值也会报错\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mIndexError\u001b[0m: list assignment index out of range"
          ]
        }
      ]
    },
    {
      "metadata": {
        "id": "r0UQiDtIxk9S",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "# 元组\n"
      ]
    },
    {
      "metadata": {
        "id": "WWuMLgG0p2cb",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "8389fd78-97d7-42db-a51b-3e6ea1f655f0",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529589778758,
          "user_tz": -480,
          "elapsed": 927,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "(1,2,3)"
      ],
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(1, 2, 3)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 1
        }
      ]
    },
    {
      "metadata": {
        "id": "9HLrFcFUp6hH",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "e05acca4-aa82-4223-b447-ff55194c5312",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529592367221,
          "user_tz": -480,
          "elapsed": 1513,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "a = (1,2,3)\n",
        "a[2]"
      ],
      "execution_count": 44,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "3"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 44
        }
      ]
    },
    {
      "metadata": {
        "id": "tcqlC08Kp_L1",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "# 字典"
      ]
    },
    {
      "metadata": {
        "id": "qRFVPG-0qC0X",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 68
        },
        "outputId": "3e4050d9-6e30-41d1-db8d-081d0a128010",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529592220785,
          "user_tz": -480,
          "elapsed": 3351,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "d = {\"a\":1, \"b\":2}\n",
        "print(d)\n",
        "\n",
        "print(d[\"a\"])\n",
        "\n",
        "d[\"a\"] = \"aaa\"\n",
        "print(d)"
      ],
      "execution_count": 43,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "{'a': 1, 'b': 2}\n",
            "1\n",
            "{'a': 'aaa', 'b': 2}\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "AMlKfmECrd3m",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "# 获取帮助\n",
        "\n",
        "在 python 中一切都是对象，他们都有自己特定可操作的方法和属性，可以使用 `dir` 进行查看有哪些可使用的方法或属性。并且可以针对方法或属性使用 `help` 函数获得对应的 doc 文档。"
      ]
    },
    {
      "metadata": {
        "id": "b-QMjfVKvbTH",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 1326
        },
        "outputId": "8e9aa815-7a07-4e01-bfa0-b262fe08f6b5",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529594332270,
          "user_tz": -480,
          "elapsed": 1148,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "dir(\"a\")"
      ],
      "execution_count": 54,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "['__add__',\n",
              " '__class__',\n",
              " '__contains__',\n",
              " '__delattr__',\n",
              " '__dir__',\n",
              " '__doc__',\n",
              " '__eq__',\n",
              " '__format__',\n",
              " '__ge__',\n",
              " '__getattribute__',\n",
              " '__getitem__',\n",
              " '__getnewargs__',\n",
              " '__gt__',\n",
              " '__hash__',\n",
              " '__init__',\n",
              " '__init_subclass__',\n",
              " '__iter__',\n",
              " '__le__',\n",
              " '__len__',\n",
              " '__lt__',\n",
              " '__mod__',\n",
              " '__mul__',\n",
              " '__ne__',\n",
              " '__new__',\n",
              " '__reduce__',\n",
              " '__reduce_ex__',\n",
              " '__repr__',\n",
              " '__rmod__',\n",
              " '__rmul__',\n",
              " '__setattr__',\n",
              " '__sizeof__',\n",
              " '__str__',\n",
              " '__subclasshook__',\n",
              " 'capitalize',\n",
              " 'casefold',\n",
              " 'center',\n",
              " 'count',\n",
              " 'encode',\n",
              " 'endswith',\n",
              " 'expandtabs',\n",
              " 'find',\n",
              " 'format',\n",
              " 'format_map',\n",
              " 'index',\n",
              " 'isalnum',\n",
              " 'isalpha',\n",
              " 'isdecimal',\n",
              " 'isdigit',\n",
              " 'isidentifier',\n",
              " 'islower',\n",
              " 'isnumeric',\n",
              " 'isprintable',\n",
              " 'isspace',\n",
              " 'istitle',\n",
              " 'isupper',\n",
              " 'join',\n",
              " 'ljust',\n",
              " 'lower',\n",
              " 'lstrip',\n",
              " 'maketrans',\n",
              " 'partition',\n",
              " 'replace',\n",
              " 'rfind',\n",
              " 'rindex',\n",
              " 'rjust',\n",
              " 'rpartition',\n",
              " 'rsplit',\n",
              " 'rstrip',\n",
              " 'split',\n",
              " 'splitlines',\n",
              " 'startswith',\n",
              " 'strip',\n",
              " 'swapcase',\n",
              " 'title',\n",
              " 'translate',\n",
              " 'upper',\n",
              " 'zfill']"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 54
        }
      ]
    },
    {
      "metadata": {
        "id": "82Hq1jR_7T9A",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "`__add__` 是内部方法，实现了字符串的拼接操作和使用 `+` 运算符效果一样"
      ]
    },
    {
      "metadata": {
        "id": "jZGoz5PrvbCi",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 51
        },
        "outputId": "bbaa7d61-8ce9-4006-9f8a-223c64dd255f",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529594355291,
          "user_tz": -480,
          "elapsed": 1740,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "print(\"a\" + 'NI!')\n",
        "print(\"a\".__add__('NI!'))"
      ],
      "execution_count": 55,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "aNI!\n",
            "aNI!\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "A0qroAg27ZcJ",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "`help` 获取帮助文档"
      ]
    },
    {
      "metadata": {
        "id": "0y4LnUT5ryHd",
        "colab_type": "code",
        "colab": {
          "autoexec": {
            "startup": false,
            "wait_interval": 0
          },
          "base_uri": "https://localhost:8080/",
          "height": 170
        },
        "outputId": "54017ecf-d090-4597-f1f1-aad258f3de74",
        "executionInfo": {
          "status": "ok",
          "timestamp": 1529592451020,
          "user_tz": -480,
          "elapsed": 1822,
          "user": {
            "displayName": "wang san",
            "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s128",
            "userId": "110912998189487813609"
          }
        }
      },
      "cell_type": "code",
      "source": [
        "help(\"a\".replace)"
      ],
      "execution_count": 48,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Help on built-in function replace:\n",
            "\n",
            "replace(...) method of builtins.str instance\n",
            "    S.replace(old, new[, count]) -> str\n",
            "    \n",
            "    Return a copy of S with all occurrences of substring\n",
            "    old replaced by new.  If the optional argument count is\n",
            "    given, only the first count occurrences are replaced.\n",
            "\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "_ud303JO0IhA",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "# 试一试\n",
        "\n",
        "- [ ] 将元组 `('a', 'bb')` 拼接 `a` ，并输出结果\n",
        "- [ ] 为字典 `d` 新增字段数据：键 `'c'`，值 `'cc'` "
      ]
    }
  ]
}